package com.share.lottery.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.share.lottery.R;
import com.share.lottery.base.BaseApplication;

/**
 * viewpage的适配器
 * 
 * @author Administrator
 * 
 */
public class AdvAdapter extends PagerAdapter {
	private String[] strs;
	private List<ImageView> views;
	private Context context;
	private BitmapUtils bitmapUtils;
	private String[] Photos; // 存放大图地址
	 public AdvAdapter(String[] strs,Context context,String[] Photos) {  
         this.strs = strs;  
         this.context=context;
         this.Photos=Photos;
         views=new ArrayList<ImageView>();
         for (int i = 0; i < strs.length; i++) {
			views.add(new ImageView(BaseApplication.getInstance()));
         }
         bitmapUtils=new BitmapUtils(context);
         bitmapUtils.configDefaultLoadFailedImage(R.drawable.advert_defaut_img);
         bitmapUtils.configDefaultLoadingImage(R.drawable.advert_defaut_img);
     }
	 

	public AdvAdapter(String[] strs, Context context) {
		super();
		this.strs = strs;
		this.context = context;
		 views=new ArrayList<ImageView>();
         for (int i = 0; i < strs.length; i++) {
			views.add(new ImageView(BaseApplication.getInstance()));
         }
         bitmapUtils=new BitmapUtils(context);
         bitmapUtils.configDefaultLoadFailedImage(R.drawable.advert_defaut_img);
	}

	/**
      * Remove a page for the given position
      * Parameters:
      *  container :The containing View from which the page will be removed. 
			position  :The page position to be removed. 
			object    :The same object that was returned by instantiateItem(View, int).  
      */
	 @Override
	public void destroyItem(View container, int position, Object object) {
		 ((ViewPager) container).removeView(views.get(position));  
	}
	  /**
      * 初始化ViewPager中的项 Create the page for the given position
      *  Parameters
			container  The containing View in which the page will be shown. 
			position  The page position to be instantiated. 

      */
	 @Override
	public Object instantiateItem(View container, final int position) {
		 bitmapUtils.display(views.get(position), strs[position]);
		 ((ViewPager) container).addView(views.get(position),0);
		 ImageView imageView=views.get(position);
		 imageView.setScaleType(ScaleType.FIT_XY);
		 imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(context,
//						ViewPagerActivity.class);
//				intent.putExtra("photos", Photos);
//				intent.putExtra("postion", position);
//				context.startActivity(intent);
				
				BaseApplication.showToast("跳转到广告详情");
			}
		});
         return imageView; 
	}

	@Override
	public int getCount() {
		if(views==null)
			return 0;
		return views.size(); 
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		  return view == object;  
	}  

}
